home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / exampleCode / opengl / cap / ogl_widget.c < prev    next >
C/C++ Source or Header  |  1996-11-11  |  3KB  |  105 lines

  1. /*
  2.  * Copyright (C) 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <X11/Intrinsic.h>
  20. #include <Xm/Xm.h>
  21. #include <GL/GLwMDrawA.h>
  22. #include <GL/gl.h>
  23. #include <GL/glx.h>
  24. #include "interfere.h"
  25.  
  26. int sten_size = 0;
  27.  
  28. static XVisualInfo * visual_info = NULL;
  29. static GLXContext rendering_context;
  30. static Widget canvas = NULL;
  31.  
  32. static int dblBuf[] = {GLX_RGBA, GLX_DOUBLEBUFFER,
  33.                        GLX_STENCIL_SIZE, 4,
  34.                        GLX_RED_SIZE, 4, GLX_GREEN_SIZE, 4, GLX_BLUE_SIZE, 4,
  35.                        None};
  36. static int sngBuf[] = {GLX_RGBA, 
  37.                        GLX_STENCIL_SIZE, 4,
  38.                        GLX_RED_SIZE, 4, GLX_GREEN_SIZE, 4, GLX_BLUE_SIZE, 4,
  39.                        None};
  40.  
  41. XVisualInfo *
  42. getVisualInfo (Display * display)
  43. {
  44.     XVisualInfo * vi = glXChooseVisual(display, DefaultScreen(display), dblBuf);
  45.     if (vi == NULL) {
  46.         vi = glXChooseVisual(display, DefaultScreen(display), sngBuf);
  47.         if (vi != NULL) {
  48.             fprintf(stderr, 
  49.                     "Could not find a double buffered visual\n");
  50.             fprintf(stderr, " -- reverting to single buffer\n");
  51.         }
  52.         else {
  53.             fprintf(stderr, "Could not find a suitable visual -- aborting\n");
  54.             exit(1);
  55.         }
  56.     }
  57.     glXGetConfig(display, vi, GLX_STENCIL_SIZE, &sten_size);
  58.     visual_info = vi;
  59.     return vi;
  60. }
  61.  
  62. static void
  63. exposeCallback (Widget w, XtPointer client, XtPointer callback)
  64. {
  65.     drawScene();
  66. }
  67.  
  68. Widget
  69. initRenderingArea (Widget parent)
  70. {
  71.     Arg args[0x2];
  72.     int width = 0, height = 0;
  73.  
  74.     XtVaGetValues(parent, XmNwidth, &width, XmNheight, &height, NULL);
  75.     canvas = XtVaCreateManagedWidget("canvas", glwMDrawingAreaWidgetClass,
  76.                                      parent, 
  77.                                      XmNwidth, width, 
  78.                                      XmNheight, height,
  79.                                      GLwNattribList, dblBuf,
  80.                                      NULL);
  81.     XtAddCallback(canvas, XmNexposeCallback, exposeCallback, NULL);
  82.  
  83.     XtVaGetValues(canvas, GLwNvisualInfo, &visual_info, NULL);
  84.     rendering_context = glXCreateContext(XtDisplay(parent), visual_info, 
  85.                                          NULL, GL_TRUE);
  86.     XtManageChild(parent);
  87.  
  88.  
  89.     return canvas;
  90. }
  91.  
  92. void
  93. setCurrentRenderingContext (Display * display)
  94. {
  95.     glXMakeCurrent(display, XtWindow(canvas), rendering_context);
  96.     initGraphics ();
  97. }
  98.  
  99. void
  100. showCurrent (void)
  101. {
  102.     glXSwapBuffers(XtDisplay(canvas), XtWindow(canvas));
  103. }
  104.  
  105.